<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
                <div class="table-handle">
                  <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增</el-button>
                </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
          >
            <el-table-column prop="id" label="ID" width="60"/>
            <el-table-column prop="key" label="配置键名" width="200" />
            <el-table-column prop="title" label="配置标题" width="200" />
            <el-table-column prop="value" label="配置值" min-width="300">
              <template slot-scope="scope">
                <div v-if="scope.row.type === 'image'">
                  <el-image
                      v-if="scope.row.value"
                      :src="scope.row.value"
                      style="width: 80px; height: 80px"
                      fit="cover"
                      :preview-src-list="[scope.row.value]">
                  </el-image>
                  <span v-else>无图片</span>
                </div>
                <div v-else-if="scope.row.type === 'textarea'">
                  <el-tooltip :content="scope.row.value" placement="top">
                    <span>{{ scope.row.value.substring(0, 50) }}{{ scope.row.value.length > 50 ? '...' : '' }}</span>
                  </el-tooltip>
                </div>
                <div v-else>
                  {{ scope.row.value }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="type" label="类型" width="100">
              <template slot-scope="scope">
                <el-tag :type="getTypeTagType(scope.row.type)">
                  {{ getTypeText(scope.row.type) }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="sort" label="排序" width="80" />
            <el-table-column prop="status" label="状态" width="100">
              <template slot-scope="scope">
                <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
                  {{ scope.row.status === 1 ? '启用' : '禁用' }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="create_time" label="创建时间" width="150" />
            <el-table-column label="操作" fixed="right" width="150">
              <template slot-scope="scope">
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
    <Add ref="addFormRef" @get-page="initData" />
  </div>
</template>
<script>

import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";

export default {
  name: "ConfigIndex",
  components: {
    Add
  },
  mixins: [crud],
  data() {
    return {
      request: http.config,
      searchList: [
        {
          label: '配置键名',
          prop: 'key',
          name: 'input',
          placeholder: '请输入配置键名'
        },
        {
          label: '配置标题',
          prop: 'title',
          name: 'input',
          placeholder: '请输入配置标题'
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          placeholder: '请选择状态',
          options: [
            { label: '启用', value: 1 },
            { label: '禁用', value: 0 }
          ]
        }
      ],
      searchParams: {

      },
      defaultParams: {
      },
      tableData: []
    }
  },
  methods: {
    getTypeText(type) {
      const typeMap = {
        text: '文本',
        image: '图片',
        textarea: '多行文本'
      }
      return typeMap[type] || type
    },
    getTypeTagType(type) {
      const typeMap = {
        text: '',
        image: 'success',
        textarea: 'warning'
      }
      return typeMap[type] || ''
    }
  }
}
</script>

<style scoped lang="scss">
.table-layout {
  margin-top: 20px;
}
.table-handle {
  margin-bottom: 20px;
}
.page-box {
  margin-top: 20px;
  text-align: right;
}
</style>
